﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mrc.Common.Extensions
{
    public static class LayuiFlowEx
    {
        /// <summary>
        /// 转为流加载类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static LayuiFlowsData<T> ToLayuiFlow<T>(this IEnumerable<T> source, int page, int pageSize)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.Count();
            result.data = source.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            result.pages = (result.count + pageSize - 1) / pageSize;
            return result;
        }
        /// <summary>
        /// 转为流加载类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static LayuiFlowsData<T> ToLayuiFlow<T>(this IQueryable<T> source, int page, int pageSize)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.Count();
            result.data = source.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            result.pages = (result.count + pageSize - 1) / pageSize;
            return result;
        }

        /// <summary>
        /// 转为流加载数据格式委托（DTO Model类与映射实体类字段不一致时使用委托处理）
        /// </summary>
        /// <typeparam name="T">DTO Model类</typeparam>
        /// <typeparam name="TSource">映射实体类</typeparam>
        /// <param name="source"></param>
        /// <param name="func">委托类</param>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">获取条数</param>
        /// <returns></returns>
        public static LayuiFlowsData<T> ToLayuiFlowFunc<T, TSource>(this IQueryable<TSource> source, Func<LayuiFlowsData<T>, List<TSource>, LayuiFlowsData<T>> func, int page, int pageSize)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.Count();
            result.pages = (result.count + pageSize - 1) / pageSize;
            return func(result, source.Skip((page - 1) * pageSize).Take(pageSize).ToList());
        }
        /// <summary>
        /// 转为流加载数据格式（DTO Model类与映射实体类字段一致时使用）
        /// </summary>
        /// <typeparam name="T">DTO Model类</typeparam>
        /// <typeparam name="TSource">映射实体类</typeparam>
        /// <param name="source"></param>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">获取条数</param>
        /// <returns></returns>
        public static LayuiFlowsData<T> ToTargetLayuiFlow<T, TSource>(this IQueryable<TSource> source, int page, int pageSize)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.Count();
            result.pages = (result.count + pageSize - 1) / pageSize;
            source.Skip((page - 1) * pageSize).Take(pageSize).ToList().ForEach(item =>
            {
                var temp = (T)Activator.CreateInstance(typeof(T));
                MapEx.CopyModel(temp, item);
                result.data.Add(temp);
            }
            );
            return result;
        }

        public static LayuiFlowsData<T> ToOtherLayuiFlowFunc<T, TSource>(this LayuiFlowsData<TSource> source, Func<LayuiFlowsData<T>, LayuiFlowsData<TSource>, LayuiFlowsData<T>> func)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.count;
            result.pages = source.pages;
            return func(result, source);
        }
        public static LayuiFlowsData<T> ToOtherLayuiFlow<T, TSource>(this LayuiFlowsData<TSource> source)
        {
            LayuiFlowsData<T> result = new LayuiFlowsData<T>();
            result.count = source.count;
            result.pages = source.pages;
            source.data.ForEach(item =>
            {
                result.data.Add((T)MapEx.CopyModel((T)Activator.CreateInstance(typeof(T)), item));
            });
            return result;
        }
    }
    public class LayuiFlowsData<T>
    {
        /// <summary>
        /// 
        /// </summary>
        public int pages { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int count { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<T> data { get; set; }

        public void initPage(int count, int pagesize)
        {
            this.count = count;
            this.pages = (this.count + pagesize - 1) / pagesize;
        }
    }
}
